<template>
  <span v-if="showExtend">
    <el-button type="text" title="点击加载扩展信息" @click="loadExtend"><i class="el-icon-set-up" />自定义扩展</el-button>
    <form-extend v-if="formExtendInfo.id !== ''" ref="formExtend" :editable="editable" @saveExtend="saveExtend" @deleteExtend="deleteExtend" @closeExtend="closeExtend" />
  </span>
</template>

<script>
import formExtend from '@/views/system/form/formApply/module/FormExtend'
import { getExtendFormInfo } from '@/api/system/formSet'

export default {
  name: 'ModuleExtend',
  components: { formExtend },
  props: {
    editable: {
      type: Boolean,
      required: true,
      default: true
    },
    sourceFormObject: {
      type: Object,
      required: true,
      default: null
    },
    moduleBusClass: {
      type: String,
      required: true,
      default: ''
    }
  },
  data() {
    return {
      // 扩展表单信息
      formExtendInfo: {
        id: ''
      }
    }
  },
  computed: {
    showExtend() {
      const busExtend = this.sourceFormObject.form.busExtend
      if ((!busExtend || busExtend.formApplyId === '') && !this.editable) {
        return false
      }
      return true
    }
  },
  created() {
    // 获取该模块对应的扩展表单信息
    getExtendFormInfo(this.moduleBusClass).then(res => {
      this.formExtendInfo = res.data
    })
  },
  methods: {
    loadExtend() {
      const busExtend = this.sourceFormObject.form.busExtend
      if (!busExtend || !this.formExtendInfo.id) {
        this.$message({
          message: '未设置扩展表单信息',
          type: 'warning'
        })
        return
      }
      // 加载扩展表单
      const _thisExtend = this.$refs.formExtend
      _thisExtend.initDataOfBefore()
      if (busExtend.formApplyId && busExtend.formApplyId === busExtend.sourceFormApplyId) {
        _thisExtend.copyData(busExtend.sourceFormApplyId)
      } else if (busExtend.formApplyId) {
        _thisExtend.viewMainData(busExtend.formApplyId)
      } else if (busExtend.sourceFormApplyId) {
        _thisExtend.copyData(busExtend.sourceFormApplyId)
      } else {
        _thisExtend.addData(this.formExtendInfo.id)
      }
    },
    saveExtend(formApplyMainData) {
      const busExtend = this.sourceFormObject.form.busExtend
      busExtend.formId = formApplyMainData.formId
      busExtend.formCode = formApplyMainData.formCode
      busExtend.formApplyId = formApplyMainData.id
      busExtend.sourceFormApplyId = ''
      busExtend.enableFlag = 'Y'
    },
    deleteExtend() {
      const busExtend = this.sourceFormObject.form.busExtend
      busExtend.formId = ''
      busExtend.formCode = ''
      busExtend.formApplyId = ''
      busExtend.sourceFormApplyId = ''
      busExtend.enableFlag = 'N'
    },
    closeExtend() {
    }
  }
}
</script>
